﻿Imports System.IO
Imports System.Xml
Imports System.Net
Imports System.Text.RegularExpressions
Imports System.Threading

Module Module1

    Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer


    Public RLPath As String = Application.StartupPath & "\realmlist.wtf"
    Public WOWPath As String = Application.StartupPath & "\wow.exe"
    Public realmlist() As String

    Public currentRealm As String

    Public Sub saveToFile(ByVal textarray() As String, ByVal filename As String)
        File.WriteAllLines(filename, textarray)
    End Sub

    Public Function readFromFile(ByVal filename As String) As String
        Dim textdata As String
        If File.Exists(filename) = True Then
            Dim tempFile As FileStream = File.Open(filename, FileMode.Open, FileAccess.Read)
            If (File.Exists(filename)) Then
                Dim rdr As StreamReader = New StreamReader(tempFile)
                textdata = rdr.ReadToEnd()
                rdr.Close()
                tempFile.Close()
            End If
            Return textdata
        End If
        Return False
    End Function

    'read realmlist.wtf to array
    Public Function loadRealmList()
        Dim textarray() As String = Split(readFromFile(RLPath), vbCrLf) 'from text to array
        Dim realms(textarray.Length) As String
        Dim counter As Int32 = 0
        For Each realm As String In textarray
            If (realm.ToLower.Contains("realmlist") = True) Then
                If realm.Contains(";") = False Then currentRealm = realm.ToLower.Replace("set", "").Replace("realmlist ", "").Trim 'current realm
                realm = realm.ToLower.Replace("set", "").Replace("realmlist", "").Replace(";", "").Trim 'only realm address
                If realm.Length > 2 Then
                    realms(counter) = realm
                    counter += 1
                End If
            End If
        Next

        Array.Resize(realms, counter)

        Return realms
    End Function

    Public Function searchInArray(ByVal array() As String, ByVal value As String)
        For Each element In array
            If element = value Then Return True 'if element found
        Next
        Return False
    End Function


    Public Function getHandleByProcess(ByVal procName As String)
        Dim WindowHandle As Integer
        Dim myprocesses As String
        For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses(System.Environment.MachineName)
            If p.MainWindowHandle.ToString <> IntPtr.Zero.ToString Then
                If (p.ProcessName = procName) Then myprocesses = myprocesses & vbCrLf & p.ProcessName : WindowHandle = p.MainWindowHandle : Exit For 'get window handle by process name
            End If
        Next p
        'MsgBox(myprocesses)
        Return WindowHandle
    End Function

    Public Sub sendK(ByVal keycode)
        Dim WindowHandle As Integer
        Dim lResult As Integer
        Const WM_CHAR = &H102
        'Const WM_CLOSE = &H10

        WindowHandle = getHandleByProcess("wow")  'get window handle
        Debug.Print(WindowHandle)

        If WindowHandle <> 0 Then
            Thread.Sleep(500)
            lResult = SendMessage(WindowHandle, WM_CHAR, KEY_CONTROL, 0) 'keydown
            'lResult = SendMessage(WindowHandle, WM_CHAR, Asc(mpqFile.Substring(i, 1)), 0) 'send keyfile
            lResult = SendMessage(WindowHandle, WM_CHAR, KEY_1, 0) 'keydown
        End If
    End Sub
End Module
